//
//  settingViewController.m
//  kankanVideo
//
//  Created by wangbo on 7/9/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "settingViewController.h"
#import "ConfigData.h"
#import "GANTracker.h"


@implementation settingViewController
@synthesize tblView,delegate;

-(void)dealloc{
    [tblView release];
    [super dealloc];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section==0) {
        return 1;
    }else if(section==1){
        return 3;
    }else{
        return 3;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 30;
}
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UILabel *lbl=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 30)];
    [lbl setBackgroundColor:[UIColor clearColor]];
    /*if (section==0) {
        lbl.text=@"     个人帐户";
    }else if(section==1){
        lbl.text=@"     系统设置";
    }else{
        lbl.text=@"     软件信息";
    }*/
    if (section==0) {
        lbl.text=@"     系统设置";
    }else if(section==1){
        lbl.text=@"     软件信息";
    }
    return lbl;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"aaaaaaaaaaaaa%d",indexPath.row);
        static NSString* CellIdentifier = @"LazyTableCell";
        UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil)
        {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
            //[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
            cell.textLabel.font=[UIFont fontWithName:@"Arial" size:15];
            
            //[str retain];
            
            //cell.accessoryView=[UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        }
        [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
        //cell setSelectionStyle:UITableViewCellSelectionStyleGray
    
        if (indexPath.row==0&&indexPath.section==0) {
            cell.textLabel.text=[NSString stringWithString:@"清除缓存"];
            //cell.detailTextLabel.text=[NSString stringWithString:@"清除本地缓存中的所有内容"];
        }/*else if(indexPath.row==0&&indexPath.section==0){
            if ([[SinaController shareInstance]isLogin]) {
                NSDictionary* dict=[[SinaController shareInstance]getSinaInfo];
                cell.textLabel.text=[dict objectForKey:@"name"];
            }else{
                cell.textLabel.text=[NSString stringWithString:@"未登录"];
            }
        }*/else if(indexPath.row==0&&indexPath.section==1){
            cell.textLabel.text=[NSString stringWithString:@"新手指南"];
            //cell.detailTextLabel.text=[NSString stringWithString:@"欢迎使用看看吧"];
        }else if(indexPath.row==1&&indexPath.section==1){
            cell.textLabel.text=[NSString stringWithString:@"版本信息  看看吧HD 0.9.1版"];
            //cell.detailTextLabel.text=[NSString stringWithString:@"看看吧iphone 0.9.1版"];
            [cell setAccessoryType:UITableViewCellAccessoryNone];
        }else if(indexPath.row==2&&indexPath.section==1){
            cell.textLabel.text=[NSString stringWithString:@"关于我们"];
            //cell.detailTextLabel.text=[NSString stringWithString:@"欢迎联系我们"];
        }
        return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row==0&&indexPath.section==0) {
        UIAlertView *alert =[[UIAlertView alloc] initWithTitle:@"删除缓存" message:@"您确定要清除系统缓存吗？" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消",nil];
        [alert show];
        alert.tag=1;
        [alert release];
        /*UIActionSheet *menu=[[UIActionSheet alloc] initWithTitle:@"您确定要清除系统缓存吗？" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"确定" otherButtonTitles: nil];
        menu.tag=1;
        AppDelegate *app=(AppDelegate*)[[UIApplication sharedApplication]delegate];
        //app.window
        [menu showInView:app.window];*/
    }else if(indexPath.row==0&&indexPath.section==1){
        [delegate clickMenu:1];
    }else if(indexPath.row==0&&indexPath.section==1111){
        if ([[SinaController shareInstance]isLogin]) {
            /*UIActionSheet *menu=[[UIActionSheet alloc] initWithTitle:@"您要注销当前帐户吗？" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"注销" otherButtonTitles: nil];
            menu.tag=2;
            [menu showInView:self.view];*/
            UIAlertView *alert =[[UIAlertView alloc] initWithTitle:@"删除登录" message:@"您要注销当前帐户吗？" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消",nil];
            [alert show];
            alert.tag=2;
            [alert release];
        }else{
            [[SinaController shareInstance] setDelegate:self];
            //AppDelegate *app=(AppDelegate*)[[UIApplication sharedApplication]delegate];
            [[SinaController shareInstance] login:self];
        }
    }else if(indexPath.row==2&&indexPath.section==1){
        [delegate clickMenu:2];
        if(!aboutus)aboutus=[[AboutUs alloc]init];
        [self.view addSubview:aboutus.view];
            NSError *error;
            if (![[GANTracker sharedTracker] trackPageview:@"看看吧-关于我们页"
                                                 withError:&error]) {
                NSLog(@"error in trackPageview");
            }
            NSLog(@"guilder_view_will_appear");
        
        //[self presentModalViewController:aboutus animated:YES];
        //[self.navigationController pushViewController:aboutus animated:YES];
    }
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

-(void)didLogin{
    [self.tblView reloadData];
}

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    NSLog(@"%d",actionSheet.tag);
    if (actionSheet.tag==1) {
        if (buttonIndex==0) {
            [[MyCoreData shareInstance]delall];
        }
    }else if(actionSheet.tag==2){
        if(buttonIndex==0){
            [[MyCoreData shareInstance]updateConfig:@"" withUserid:@"" withName:@""];
            [self.tblView reloadData];
        }
    }
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    //NSLog(@"%@:%d",alertView.title,buttonIndex);
    if (alertView.tag==1) {
        if (buttonIndex==0) {
            [[MyCoreData shareInstance]delall];
        }
    }else if(alertView.tag==2){
        if(buttonIndex==0){
            [[MyCoreData shareInstance]updateConfig:@"" withUserid:@"" withName:@""];
            [self.tblView reloadData];
        }
    }
    /*if (buttonIndex==0) {
        AppDelegate *app=(AppDelegate*)[[UIApplication sharedApplication]delegate];
        [app showAlert:@"正在删除"];
        //[[MyCoreData shareInstance] setDelegate:self];
        [[MyCoreData shareInstance]delall];
        [self performSelector:@selector(hideAlert2) withObject:nil afterDelay:3];
    }*/
}

-(void)hideAlert2{
    AppDelegate *app=(AppDelegate*)[[UIApplication sharedApplication]delegate];
    [app hideAlert];
}

-(void)processComplete{
    
}

-(IBAction)btnLogin:(id)sender{
    if(!loginView)loginView=[[LoginViewController alloc]init];
    [self.navigationController pushViewController:loginView animated:YES];
}

-(IBAction)back:(id)sender{
    //[self.navigationController popViewControllerAnimated:YES];
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.5];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationDidStopSelector:@selector(animationComplete)];
    //sv.contentOffset=CGPointMake(320*nowPage,0);
    [self.view setFrame:CGRectMake(1024, 0, 500, 748)];
    [UIView commitAnimations];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    tblView.delegate=self;
    tblView.dataSource=self;
    [tblView reloadData];
}



- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
